Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms

Imports System.Drawing.Design
Imports System.Drawing.Drawing2D

Namespace xpComponent.Form
    Public Class emunType
        Public Enum XPStyle
            [Default]
            Blue
            OliveGreen
            Silver
        End Enum

        Public Enum BtnShape
            Rectangle
            Ellipse
        End Enum
    End Class

    Public Class xpButton
        Inherits System.Windows.Forms.Button

        Public Enum ControlState
            Normal 'The XP control is in the normal state.
            Hover 'The XP control is in the hover state.
            Pressed 'The XP control is in the pressed state.
            [Default] 'The XP control object is in the default state.
            Disabled 'The XP control object is in the disabled state.
        End Enum

#Region "Component Designer generated code  "

        Private components As System.ComponentModel.Container = Nothing

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows.Forms Form Designer.
            InitializeComponent()

            Me.SetStyle(ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer, True)

            'Add any initialization after the InitComponent call

        End Sub

        'Required method for Designer support - do not modify 
        'the contents of this method with the code editor.
        Private Sub InitializeComponent()
            components = New System.ComponentModel.Container
        End Sub

        'Clean up any resources being used.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing) Then
                If (Not components Is Nothing) Then
                    components.Dispose()
                End If
            End If

            MyBase.Dispose(disposing)
        End Sub
#End Region

#Region "Instance fields            "
        Private enmState As ControlState = ControlState.Normal
        Private bCanClick As Boolean = False
        Private locPoint As Point
#End Region

#Region "Static members             "
        'Initializes all static fields of the XpButton class.
        Private Shared ReadOnly sizeBorderPixelIndent As Size
        Private Shared ReadOnly clrOuterShadow1 As Color
        Private Shared ReadOnly clrOuterShadow2 As Color
        Private Shared ReadOnly clrBackground1 As Color
        Private Shared ReadOnly clrBackground2 As Color
        Private Shared ReadOnly clrBorder As Color
        Private Shared ReadOnly clrInnerShadowBottom1 As Color
        Private Shared ReadOnly clrInnerShadowBottom2 As Color
        Private Shared ReadOnly clrInnerShadowBottom3 As Color
        Private Shared ReadOnly clrInnerShadowRight1a As Color
        Private Shared ReadOnly clrInnerShadowRight1b As Color
        Private Shared ReadOnly clrInnerShadowRight2a As Color
        Private Shared ReadOnly clrInnerShadowRight2b As Color
        Private Shared ReadOnly clrInnerShadowBottomPressed1 As Color
        Private Shared ReadOnly clrInnerShadowBottomPressed2 As Color
        Private Shared ReadOnly clrInnerShadowTopPressed1 As Color
        Private Shared ReadOnly clrInnerShadowTopPressed2 As Color
        Private Shared ReadOnly clrInnerShadowLeftPressed1 As Color
        Private Shared ReadOnly clrInnerShadowLeftPressed2 As Color
#End Region

#Region "Constructors               "
        'Initializes a new instance of the XpButton class.
        Shared Sub New()
            sizeBorderPixelIndent = New Size(4, 4)

            'Normal colors
            clrOuterShadow1 = Color.FromArgb(64, 164, 164, 164)
            clrOuterShadow2 = Color.FromArgb(64, Color.White)
            clrBackground1 = Color.FromArgb(250, 250, 248)
            clrBackground2 = Color.FromArgb(240, 240, 234)
            clrBorder = Color.FromArgb(0, 60, 116)
            clrInnerShadowBottom1 = Color.FromArgb(236, 235, 230)
            clrInnerShadowBottom2 = Color.FromArgb(226, 223, 214)
            clrInnerShadowBottom3 = Color.FromArgb(214, 208, 197)
            clrInnerShadowRight1a = Color.FromArgb(128, 236, 234, 230)
            clrInnerShadowRight1b = Color.FromArgb(128, 224, 220, 212)
            clrInnerShadowRight2a = Color.FromArgb(128, 234, 228, 218)
            clrInnerShadowRight2b = Color.FromArgb(128, 212, 208, 196)

            'Pressed colors
            clrInnerShadowBottomPressed1 = Color.FromArgb(234, 233, 227)
            clrInnerShadowBottomPressed2 = Color.FromArgb(242, 241, 238)
            clrInnerShadowTopPressed1 = Color.FromArgb(209, 204, 193)
            clrInnerShadowTopPressed2 = Color.FromArgb(220, 216, 207)
            clrInnerShadowLeftPressed1 = Color.FromArgb(216, 213, 203)
            clrInnerShadowLeftPressed2 = Color.FromArgb(222, 220, 211)
        End Sub
#End Region

#Region "Properties                 "
        Public Shadows Property FlatStyle() As FlatStyle
            Get
                Return MyBase.FlatStyle
            End Get
            Set(ByVal Value As FlatStyle)
                MyBase.FlatStyle = FlatStyle.Standard
            End Set
        End Property

        Private m_btnStyle As emunType.XPStyle = emunType.XPStyle.Default
        Private m_btnShape As emunType.BtnShape = emunType.BtnShape.Rectangle
        Public Property BtnShape() As emunType.BtnShape
            Get
                Return m_btnShape
            End Get
            Set(ByVal Value As emunType.BtnShape)
                m_btnShape = Value
                MyBase.Invalidate()
            End Set
        End Property

        <DefaultValue("Blue"), System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)> _
        Public Property BtnStyle() As emunType.XPStyle
            Get
                Return m_btnStyle
            End Get
            Set(ByVal Value As emunType.XPStyle)
                m_btnStyle = Value
                Me.Invalidate()
            End Set
        End Property

        Public Property AdjustImageLocation() As Point
            Get
                Return locPoint
            End Get
            Set(ByVal Value As Point)
                locPoint = Value
                Me.Invalidate()
            End Set
        End Property

        '<value>Gets the clipping rectangle of the XpButton object's border.</value>
        Private ReadOnly Property BorderRectangle() As Rectangle
            Get
                Dim rc As Rectangle = Me.ClientRectangle
                Return New Rectangle(1, 1, rc.Width - 3, rc.Height - 3)
            End Get
        End Property
#End Region

#Region "Methods                    "
        'Overridden Event Handlers
        Protected Overrides Sub OnClick(ByVal ea As EventArgs)
            Me.Capture = False
            bCanClick = False

            If Me.ClientRectangle.Contains(Me.PointToClient(Control.MousePosition)) Then
                enmState = ControlState.Hover
            Else
                enmState = ControlState.Normal
            End If
            Me.Invalidate()

            MyBase.OnClick(ea)
        End Sub

        Protected Overrides Sub OnMouseEnter(ByVal ea As EventArgs)
            MyBase.OnMouseEnter(ea)

            enmState = ControlState.Hover
            Me.Invalidate()
        End Sub

        Protected Overrides Sub OnMouseDown(ByVal mea As MouseEventArgs)
            MyBase.OnMouseDown(mea)

            If (mea.Button = MouseButtons.Left) Then
                bCanClick = True
                enmState = ControlState.Pressed
                Me.Invalidate()
            End If
        End Sub

        Protected Overrides Sub OnMouseMove(ByVal mea As MouseEventArgs)
            MyBase.OnMouseMove(mea)

            If (ClientRectangle.Contains(mea.X, mea.Y)) Then
                If (enmState = ControlState.Hover And Me.Capture And Not bCanClick) Then
                    bCanClick = True
                    enmState = ControlState.Pressed
                    Me.Invalidate()
                End If
            Else
                If (enmState = ControlState.Pressed) Then
                    bCanClick = False
                    enmState = ControlState.Hover
                    Me.Invalidate()
                End If
            End If
        End Sub

        Protected Overrides Sub OnMouseLeave(ByVal ea As EventArgs)
            MyBase.OnMouseLeave(ea)

            enmState = ControlState.Normal
            Me.Invalidate()
        End Sub

        Protected Overrides Sub OnPaint(ByVal pea As PaintEventArgs)
            Me.OnPaintBackground(pea)

            Select Case (enmState)
                Case ControlState.Normal
                    If (Me.Enabled) Then
                        If (Me.Focused Or Me.IsDefault) Then
                            Select Case (m_btnShape)
                                Case emunType.BtnShape.Rectangle
                                    OnDrawDefault(pea.Graphics)
                                Case emunType.BtnShape.Ellipse
                                    OnDrawDefaultEllipse(pea.Graphics)
                            End Select
                        Else
                            Select Case (m_btnShape)
                                Case emunType.BtnShape.Rectangle
                                    OnDrawNormal(pea.Graphics)
                                Case emunType.BtnShape.Ellipse
                                    OnDrawNormalEllipse(pea.Graphics)
                            End Select
                        End If
                    Else
                        OnDrawDisabled(pea.Graphics)
                    End If
                Case ControlState.Hover
                    Select Case (m_btnShape)
                        Case emunType.BtnShape.Rectangle
                            OnDrawHover(pea.Graphics)

                        Case emunType.BtnShape.Ellipse
                            OnDrawHoverEllipse(pea.Graphics)
                    End Select
                Case ControlState.Pressed
                    Select Case (m_btnShape)
                        Case emunType.BtnShape.Rectangle
                            OnDrawPressed(pea.Graphics)
                        Case emunType.BtnShape.Ellipse
                            OnDrawPressedEllipse(pea.Graphics)
                    End Select
            End Select

            OnDrawTextAndImage(pea.Graphics)
        End Sub

        Protected Overrides Sub OnEnabledChanged(ByVal ea As EventArgs)
            MyBase.OnEnabledChanged(ea)
            enmState = ControlState.Normal
            Me.Invalidate()
        End Sub

        ' <summary>
        'Draws the normal state of the XpButton.
        ' </summary>
        '<param name="g">The System.Drawing.Graphics object to be used to paint the XpButton.</param>
        Private Sub OnDrawNormal(ByVal g As Graphics)
            DrawNormalButton(g)
            'no need to call base class implementation
        End Sub

        Private Sub OnDrawHoverEllipse(ByVal g As Graphics)
            DrawNormalEllipse(g)
            DrawEllipseHoverBorder(g)
            DrawEllipseBorder(g)
        End Sub

        '<summary>
        'Draws the hover state of the XpButton.
        '</summary>
        '<param name="g">The System.Drawing.Graphics object to be used to paint the XpButton.</param>
        Private Sub OnDrawHover(ByVal g As Graphics)
            DrawNormalButton(g)

            ' Need to draw only the "thick border" for hover buttons
            Dim rcBorder As Rectangle = Me.BorderRectangle

            'Top
            Dim penTop1 As Pen = New Pen(Color.FromArgb(255, 240, 207))
            Dim penTop2 As Pen = New Pen(Color.FromArgb(253, 216, 137))

            g.DrawLine(penTop1, rcBorder.Left + 2, rcBorder.Top + 1, rcBorder.Right - 2, rcBorder.Top + 1)
            g.DrawLine(penTop2, rcBorder.Left + 1, rcBorder.Top + 2, rcBorder.Right - 1, rcBorder.Top + 2)

            penTop1.Dispose()
            penTop2.Dispose()

            'Bottom
            Dim penBottom1 As Pen = New Pen(Color.FromArgb(248, 178, 48))
            Dim penBottom2 As Pen = New Pen(Color.FromArgb(229, 151, 0))

            g.DrawLine(penBottom1, rcBorder.Left + 1, rcBorder.Bottom - 2, rcBorder.Right - 1, rcBorder.Bottom - 2)
            g.DrawLine(penBottom2, rcBorder.Left + 2, rcBorder.Bottom - 1, rcBorder.Right - 2, rcBorder.Bottom - 1)

            penBottom1.Dispose()
            penBottom2.Dispose()

            'Left and Right
            Dim rcLeft As Rectangle = New Rectangle(rcBorder.Left + 1, rcBorder.Top + 3, 2, rcBorder.Height - 5)
            Dim rcRight As Rectangle = New Rectangle(rcBorder.Right - 2, rcBorder.Top + 3, 2, rcBorder.Height - 5)
            Dim brushSide As LinearGradientBrush = New LinearGradientBrush(rcLeft, Color.FromArgb(254, 221, 149), Color.FromArgb(249, 180, 53), LinearGradientMode.Vertical)

            g.FillRectangle(brushSide, rcLeft)
            g.FillRectangle(brushSide, rcRight)

            brushSide.Dispose()
        End Sub

        Private Sub OnDrawPressedEllipse(ByVal g As Graphics)
            DrawPressedEllipse(g)
            DrawEllipseBorder(g)
        End Sub

        Private Sub DrawPressedEllipse(ByVal g As Graphics)
            Dim rcBorder As Rectangle = Me.BorderRectangle
            Dim rcBackground As Rectangle = New Rectangle(rcBorder.X + 1, rcBorder.Y + 1, rcBorder.Width - 1, rcBorder.Height - 1)
            Dim brushBackground As SolidBrush = New SolidBrush(Color.FromArgb(226, 225, 218))
            'Draw an ellipse to the screen using the LinearGradientBrush.
            g.FillEllipse(brushBackground, rcBackground)
            'Create a triangular shaped brush with the peak at the center of the drawing area.
        End Sub

        '<summary>
        'Draws the pressed state of the XpButton.
        '</summary>
        '<param name="g">The System.Drawing.Graphics object to be used to paint the XpButton.</param>
        Private Sub OnDrawPressed(ByVal g As Graphics)
            Dim rcBorder As Rectangle = Me.BorderRectangle

            'Outer shadow
            DrawOuterShadow(g)

            'Background
            Dim rcBackground As Rectangle = New Rectangle(rcBorder.X + 1, rcBorder.Y + 1, rcBorder.Width - 1, rcBorder.Height - 1)
            Dim brushBackground As SolidBrush = New SolidBrush(Color.FromArgb(226, 225, 218))
            g.FillRectangle(brushBackground, rcBackground)
            brushBackground.Dispose()

            'Border
            DrawBorder(g)

            'Inner shadow above the bottom border (2 solid lines)
            Dim penInnerShadowBottomPressed1 As Pen = New Pen(clrInnerShadowBottomPressed1)
            Dim penInnerShadowBottomPressed2 As Pen = New Pen(clrInnerShadowBottomPressed2)

            g.DrawLine(penInnerShadowBottomPressed1, rcBorder.Left + 1, rcBorder.Bottom - 2, rcBorder.Right - 1, rcBorder.Bottom - 2)
            g.DrawLine(penInnerShadowBottomPressed2, rcBorder.Left + 2, rcBorder.Bottom - 1, rcBorder.Right - 2, rcBorder.Bottom - 1)

            penInnerShadowBottomPressed1.Dispose()
            penInnerShadowBottomPressed2.Dispose()

            'Inner shadow below the top border (2 solid lines)
            Dim penInnerShadowTopPressed1 As Pen = New Pen(clrInnerShadowTopPressed1)
            Dim penInnerShadowTopPressed2 As Pen = New Pen(clrInnerShadowTopPressed2)

            g.DrawLine(penInnerShadowTopPressed1, rcBorder.Left + 2, rcBorder.Top + 1, rcBorder.Right - 2, rcBorder.Top + 1)
            g.DrawLine(penInnerShadowTopPressed2, rcBorder.Left + 1, rcBorder.Top + 2, rcBorder.Right - 1, rcBorder.Top + 2)

            penInnerShadowTopPressed1.Dispose()
            penInnerShadowTopPressed2.Dispose()

            'Inner shadow right the left border (2 solid lines)
            Dim penInnerShadowLeftPressed1 As Pen = New Pen(clrInnerShadowLeftPressed1)
            Dim penInnerShadowLeftPressed2 As Pen = New Pen(clrInnerShadowLeftPressed2)

            g.DrawLine(penInnerShadowLeftPressed1, rcBorder.Left + 1, rcBorder.Top + 3, rcBorder.Left + 1, rcBorder.Bottom - 3)
            g.DrawLine(penInnerShadowLeftPressed2, rcBorder.Left + 2, rcBorder.Top + 3, rcBorder.Left + 2, rcBorder.Bottom - 3)

            penInnerShadowLeftPressed1.Dispose()
            penInnerShadowLeftPressed2.Dispose()
        End Sub

        '<summary>
        'Draws the default state of the XpButton.
        '</summary>
        '<param name="g">The System.Drawing.Graphics object to be used to paint the XpButton.</param>
        Private Sub OnDrawNormalEllipse(ByVal g As Graphics)
            DrawNormalEllipse(g)
            DrawEllipseBorder(g)
        End Sub

        Private Sub OnDrawDefaultEllipse(ByVal g As Graphics)
            DrawNormalEllipse(g)
            DrawEllipseDefaultBorder(g)
            DrawEllipseBorder(g)
        End Sub

        Private Sub OnDrawDefault(ByVal g As Graphics)
            DrawNormalButton(g)

            'Need to draw only the "thick border" for default buttons

            Dim rcBorder As Rectangle = Me.BorderRectangle

            'Top
            Dim penTop1 As Pen = New Pen(Color.FromArgb(206, 231, 255))
            Dim penTop2 As Pen = New Pen(Color.FromArgb(188, 212, 246))

            g.DrawLine(penTop1, rcBorder.Left + 2, rcBorder.Top + 1, rcBorder.Right - 2, rcBorder.Top + 1)
            g.DrawLine(penTop2, rcBorder.Left + 1, rcBorder.Top + 2, rcBorder.Right - 1, rcBorder.Top + 2)

            penTop1.Dispose()
            penTop2.Dispose()

            'Bottom
            Dim penBottom1 As Pen = New Pen(Color.FromArgb(137, 173, 228))
            Dim penBottom2 As Pen = New Pen(Color.FromArgb(105, 130, 238))

            g.DrawLine(penBottom1, rcBorder.Left + 1, rcBorder.Bottom - 2, rcBorder.Right - 1, rcBorder.Bottom - 2)
            g.DrawLine(penBottom2, rcBorder.Left + 2, rcBorder.Bottom - 1, rcBorder.Right - 2, rcBorder.Bottom - 1)

            penBottom1.Dispose()
            penBottom2.Dispose()

            'Left and Right
            Dim rcLeft As Rectangle = New Rectangle(rcBorder.Left + 1, rcBorder.Top + 3, 2, rcBorder.Height - 5)
            Dim rcRight As Rectangle = New Rectangle(rcBorder.Right - 2, rcBorder.Top + 3, 2, rcBorder.Height - 5)

            Dim brushSide As LinearGradientBrush = New LinearGradientBrush(rcLeft, Color.FromArgb(186, 211, 245), Color.FromArgb(137, 173, 228), LinearGradientMode.Vertical)

            g.FillRectangle(brushSide, rcLeft)
            g.FillRectangle(brushSide, rcRight)

            brushSide.Dispose()
        End Sub

        '<summary>
        'Draws the disabled state of the XpButton.
        '</summary>
        '<param name="g">The System.Drawing.Graphics object to be used to paint the XpButton.</param>
        Private Sub OnDrawDisabled(ByVal g As Graphics)
            Dim rcBorder As Rectangle = Me.BorderRectangle

            'Background
            Dim rcBackground As Rectangle = New Rectangle(rcBorder.X + 1, rcBorder.Y + 1, rcBorder.Width - 1, rcBorder.Height - 1)
            Dim brushBackground As SolidBrush = New SolidBrush(Color.FromArgb(245, 244, 234))

            g.FillRectangle(brushBackground, rcBackground)
            brushBackground.Dispose()

            'Border
            Dim penBorder As Pen = New Pen(Color.FromArgb(201, 199, 186))
            ControlPaint.DrawRoundedRectangle(g, penBorder, rcBorder, sizeBorderPixelIndent)

            penBorder.Dispose()
        End Sub

        '<summary>
        'Draws the text of the XpButton.
        '</summary>
        '<param name="g">The System.Drawing.Graphics object to be used to paint the XpButton.</param>
        Private Sub OnDrawTextAndImage(ByVal g As Graphics)
            Dim brushText As SolidBrush

            If (Enabled) Then
                brushText = New SolidBrush(ForeColor)
            Else
                brushText = New SolidBrush(ControlPaint.DisabledForeColor)
            End If

            Dim sf As StringFormat = ControlPaint.GetStringFormat(Me.TextAlign)
            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show

            If (Not Me.Image Is Nothing) Then
                Dim rc As RectangleF = New RectangleF
                Dim ImagePoint As Point = New Point(6, 4)
                Select Case Me.ImageAlign
                    Case ContentAlignment.MiddleRight
                        rc.Width = Me.ClientRectangle.Width - Me.Image.Width - 8
                        rc.Height = Me.ClientRectangle.Height
                        rc.X = 0
                        rc.Y = 0
                        ImagePoint.X = rc.Width
                        ImagePoint.Y = Me.ClientRectangle.Height / 2 - Image.Height / 2
                    Case ContentAlignment.TopCenter
                        ImagePoint.Y = 2
                        ImagePoint.X = (Me.ClientRectangle.Width - Me.Image.Width) / 2
                        rc.Width = Me.ClientRectangle.Width
                        rc.Height = Me.ClientRectangle.Height - Me.Image.Height - 4
                        rc.X = Me.ClientRectangle.X
                        rc.Y = Me.Image.Height
                    Case ContentAlignment.MiddleCenter
                        ImagePoint.X = (Me.ClientRectangle.Width - Me.Image.Width) / 2
                        ImagePoint.Y = (Me.ClientRectangle.Height - Me.Image.Height) / 2
                        rc.Width = 0
                        rc.Height = 0
                        rc.X = Me.ClientRectangle.Width
                        rc.Y = Me.ClientRectangle.Height
                    Case Else
                        ImagePoint.X = 6
                        ImagePoint.Y = Me.ClientRectangle.Height / 2 - Image.Height / 2
                        rc.Width = Me.ClientRectangle.Width - Me.Image.Width
                        rc.Height = Me.ClientRectangle.Height
                        rc.X = Me.Image.Width
                        rc.Y = 0
                End Select

                ImagePoint.X += locPoint.X
                ImagePoint.Y += locPoint.Y
                If (Me.Enabled) Then
                    g.DrawImage(Me.Image, ImagePoint)
                Else
                    System.Windows.Forms.ControlPaint.DrawImageDisabled(g, Me.Image, locPoint.X, locPoint.Y, Me.BackColor)
                End If
                If (ContentAlignment.MiddleCenter <> Me.ImageAlign) Then
                    g.DrawString(Me.Text, Me.Font, brushText, rc, sf)
                End If
            Else
                'g.DrawString(Me.Text, Me.Font, brushText, Me.ClientRectangle, sf)
            End If

            brushText.Dispose()
            sf.Dispose()
        End Sub

        Private Sub DrawNormalEllipse(ByVal g As Graphics)
            Dim rcBackground As Rectangle = Me.BorderRectangle
            Dim brushBackground As LinearGradientBrush = Nothing
            Select Case (m_btnStyle)
                Case emunType.XPStyle.Default
                    brushBackground = New LinearGradientBrush(rcBackground, clrBackground1, clrBackground2, LinearGradientMode.Vertical)
                Case emunType.XPStyle.Blue
                    brushBackground = New LinearGradientBrush(rcBackground, Color.FromArgb(248, 252, 253), Color.FromArgb(172, 171, 201), LinearGradientMode.Vertical)
                Case emunType.XPStyle.OliveGreen
                    brushBackground = New LinearGradientBrush(rcBackground, Color.FromArgb(250, 250, 240), Color.FromArgb(235, 220, 190), LinearGradientMode.Vertical)
                Case emunType.XPStyle.Silver
                    brushBackground = New LinearGradientBrush(rcBackground, Color.FromArgb(253, 253, 253), Color.FromArgb(205, 205, 205), LinearGradientMode.Vertical)
            End Select
            Dim relativeIntensities() As Single = {0.0F, 0.008F, 1.0F}
            Dim relativePositions() As Single = {0.0F, 0.22F, 1.0F}

            Dim blend As blend = New blend
            blend.Factors = relativeIntensities
            blend.Positions = relativePositions
            brushBackground.Blend = blend
            'Create a triangular shaped brush with the peak at the center of the drawing area.
            '
            'brushBackground.SetBlendTriangularShape(.5f, 1.0f)
            'Use the triangular brush to draw a second ellipse.
            'rcBackground.Y = 150
            g.FillEllipse(brushBackground, rcBackground)
        End Sub

        '<summary>
        'Draws the ordinary look of the XpButton object.
        ' </summary>
        ' <param name="g">The System.Drawing.Graphics object to be used to paint the XpButton.</param>
        Private Sub DrawNormalButton(ByVal g As Graphics)
            Dim rcBorder As Rectangle = Me.BorderRectangle

            'Outer shadow
            DrawOuterShadow(g)

            'Background
            Dim rcBackground As Rectangle = New Rectangle(rcBorder.X + 1, rcBorder.Y + 1, rcBorder.Width - 1, rcBorder.Height - 1)
            Dim brushBackground As LinearGradientBrush = Nothing
            Select Case (m_btnStyle)
                Case emunType.XPStyle.Default
                    brushBackground = New LinearGradientBrush(rcBackground, clrBackground1, clrBackground2, LinearGradientMode.Vertical)
                Case emunType.XPStyle.Blue
                    brushBackground = New LinearGradientBrush(rcBackground, Color.FromArgb(248, 252, 253), Color.FromArgb(172, 171, 201), LinearGradientMode.Vertical)
                Case emunType.XPStyle.OliveGreen
                    brushBackground = New LinearGradientBrush(rcBackground, Color.FromArgb(250, 250, 240), Color.FromArgb(235, 220, 190), LinearGradientMode.Vertical)
                Case emunType.XPStyle.Silver
                    brushBackground = New LinearGradientBrush(rcBackground, Color.FromArgb(253, 253, 253), Color.FromArgb(205, 205, 205), LinearGradientMode.Vertical)
            End Select

            Dim relativeIntensities() As Single = {0.0F, 0.08F, 1.0F}
            Dim relativePositions() As Single = {0.0F, 0.32F, 1.0F}

            Dim blend As blend = New blend
            blend.Factors = relativeIntensities
            blend.Positions = relativePositions
            brushBackground.Blend = blend

            g.FillRectangle(brushBackground, rcBackground)
            brushBackground.Dispose()

            'Border
            DrawBorder(g)

            If emunType.XPStyle.Default = m_btnStyle Then
                'Inner shadow above the bottom border (3 solid lines)
                Dim penInnerShadowBottom1 As Pen = New Pen(clrInnerShadowBottom1)
                Dim penInnerShadowBottom2 As Pen = New Pen(clrInnerShadowBottom2)
                Dim penInnerShadowBottom3 As Pen = New Pen(clrInnerShadowBottom3)

                g.DrawLine(penInnerShadowBottom1, rcBorder.Left + 1, rcBorder.Bottom - 3, rcBorder.Right - 1, rcBorder.Bottom - 3)
                g.DrawLine(penInnerShadowBottom2, rcBorder.Left + 1, rcBorder.Bottom - 2, rcBorder.Right - 1, rcBorder.Bottom - 2)
                g.DrawLine(penInnerShadowBottom3, rcBorder.Left + 2, rcBorder.Bottom - 1, rcBorder.Right - 2, rcBorder.Bottom - 1)

                penInnerShadowBottom1.Dispose()
                penInnerShadowBottom2.Dispose()
                penInnerShadowBottom3.Dispose()

                'Inner shadow to the left of the right border (2 gradient lines)
                Dim ptInnerShadowRight1a As Point = New Point(rcBorder.Right - 2, rcBorder.Top + 1)
                Dim ptInnerShadowRight1b As Point = New Point(rcBorder.Right - 2, rcBorder.Bottom - 1)
                Dim ptInnerShadowRight2a As Point = New Point(rcBorder.Right - 1, rcBorder.Top + 2)
                Dim ptInnerShadowRight2b As Point = New Point(rcBorder.Right - 1, rcBorder.Bottom - 2)

                Dim brushInnerShadowRight1 As LinearGradientBrush = New LinearGradientBrush(ptInnerShadowRight1a, ptInnerShadowRight1b, clrInnerShadowRight1a, clrInnerShadowRight1b)
                Dim penInnerShadowRight1 As Pen = New Pen(brushInnerShadowRight1)

                Dim brushInnerShadowRight2 As LinearGradientBrush = New LinearGradientBrush(ptInnerShadowRight2a, ptInnerShadowRight2b, clrInnerShadowRight2a, clrInnerShadowRight2b)
                Dim penInnerShadowRight2 As Pen = New Pen(brushInnerShadowRight2)

                g.DrawLine(penInnerShadowRight1, ptInnerShadowRight1a, ptInnerShadowRight1b)
                g.DrawLine(penInnerShadowRight2, ptInnerShadowRight2a, ptInnerShadowRight2b)

                penInnerShadowRight1.Dispose()
                penInnerShadowRight2.Dispose()
                brushInnerShadowRight1.Dispose()
                brushInnerShadowRight2.Dispose()

                'Top showing light source
                Dim penTop As Pen = New Pen(Color.White)
                g.DrawLine(penTop, rcBorder.Left + 2, rcBorder.Top + 1, rcBorder.Right - 2, rcBorder.Top + 1)
                g.DrawLine(penTop, rcBorder.Left + 1, rcBorder.Top + 2, rcBorder.Right - 1, rcBorder.Top + 2)
                g.DrawLine(penTop, rcBorder.Left + 1, rcBorder.Top + 3, rcBorder.Right - 1, rcBorder.Top + 3)
                penTop.Dispose()
            End If
        End Sub

        '<summary>
        'Draws the outer shadow of the XpButton object.
        '</summary>
        '<param name="g">The System.Drawing.Graphics object to be used to paint the outer shadow.</param>
        Private Sub DrawOuterShadow(ByVal g As Graphics)
            Dim brushOuterShadow As LinearGradientBrush = New LinearGradientBrush(ClientRectangle, clrOuterShadow1, clrOuterShadow2, LinearGradientMode.Vertical)
            g.FillRectangle(brushOuterShadow, ClientRectangle)
            brushOuterShadow.Dispose()
        End Sub

        Private Sub DrawEllipseOuterShadow(ByVal g As Graphics)
            Dim brushOuterShadow As LinearGradientBrush = New LinearGradientBrush(ClientRectangle, clrOuterShadow1, clrOuterShadow2, LinearGradientMode.Vertical)
            g.FillRectangle(brushOuterShadow, ClientRectangle)
            brushOuterShadow.Dispose()
        End Sub

        '<summary>
        'Draws the dark blue border of the XpButton object.
        '</summary>
        '<param name="g">The System.Drawing.Graphics object to be used to paint the border.</param>
        Private Sub DrawBorder(ByVal g As Graphics)
            Dim penBorder As Pen = New Pen(clrBorder)
            ControlPaint.DrawRoundedRectangle(g, penBorder, Me.BorderRectangle, sizeBorderPixelIndent)
            penBorder.Dispose()
        End Sub

        Private Sub DrawEllipseBorder(ByVal g As Graphics)
            Dim penBorder As Pen = New Pen(Color.FromArgb(0, 0, 0))

            Dim oldSmoothingMode As SmoothingMode = g.SmoothingMode
            g.SmoothingMode = SmoothingMode.AntiAlias
            g.DrawEllipse(penBorder, Me.BorderRectangle)
            g.SmoothingMode = oldSmoothingMode

            penBorder.Dispose()
        End Sub

        Private Sub DrawEllipseDefaultBorder(ByVal g As Graphics)
            Dim penTop2 As Pen = New Pen(Color.FromArgb(137, 173, 228), 2)
            Dim rcInFrame As Rectangle = New Rectangle(Me.BorderRectangle.X + 2, Me.BorderRectangle.Y + 1, Me.BorderRectangle.Width - 4, Me.BorderRectangle.Height - 2)

            Dim oldSmoothingMode As SmoothingMode = g.SmoothingMode
            g.SmoothingMode = SmoothingMode.AntiAlias
            g.DrawEllipse(penTop2, rcInFrame)
            g.SmoothingMode = oldSmoothingMode

            penTop2.Dispose()
        End Sub

        Private Sub DrawEllipseHoverBorder(ByVal g As Graphics)
            Dim penTop2 As Pen = New Pen(Color.FromArgb(248, 178, 48), 2)
            Dim rcInFrame As Rectangle = New Rectangle(Me.BorderRectangle.X + 2, Me.BorderRectangle.Y + 1, Me.BorderRectangle.Width - 4, Me.BorderRectangle.Height - 2)

            Dim oldSmoothingMode As SmoothingMode = g.SmoothingMode
            g.SmoothingMode = SmoothingMode.AntiAlias
            g.DrawEllipse(penTop2, rcInFrame)
            g.SmoothingMode = oldSmoothingMode

            penTop2.Dispose()
        End Sub
#End Region
    End Class
End Namespace